<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Goods;
use App\Models\GoodsCategory;
use Illuminate\Http\Request;

class GoodsController extends Controller
{
    // 列表
    public function index(Request $request)
    {
        $searchData = $request->post();

        $model = Goods::orderBy('goods.id', 'DESC')
            ->leftJoin('users', 'users.id', '=', 'goods.user_id')
            ->select(['goods.*', 'users.nickname']);
        if ($nickname = $searchData['nickname'] ?? '') {
            $model->where('nickname', 'like', "%{$nickname}%");
        }
        if ($name = $searchData['name'] ?? '') {
            $model->where('goods.name', 'like', "%{$name}%");
        }
        if ($category_id = $searchData['category_id'] ?? '') {
            $model->where('category_id', $category_id);
        }
        $is_rec = $searchData['is_rec'] ?? -1;
        if ($is_rec >= 0) {
            $model->where('goods.is_rec', $is_rec);
        }
        if ($type = $searchData['type'] ?? '') {
            $model->where('goods.type', $type);
        }
        $result = $model->paginate(config('app.limit'));

        return $this->writeJson(0, [
            'data' => $result->items(),
            'count' => $result->total()
        ]);
    }

    // 列表页面的参数
    public function params()
    {
        $categoryList = GoodsCategory::get([
            'id', 'name'
        ]);

        return $this->writeJson(0, [
            'categoryList' => $categoryList,
        ]);
    }

    // 新增/编辑
    public function add(Request $request)
    {
        $data = $request->post();
        if ($data['id']) {
            $model = Goods::find($data['id']);
            $model->setRawAttributes($data);
            $model->save($data);
            return $this->writeJson(0, null, '编辑成功');
        } else {
            // 新增
            unset($data['id']);
            $model = new Goods();
            $model->setRawAttributes($data);
            $model->save();
            return $this->writeJson(0, null, '新增成功');
        }
    }

    // 删除
    public function del(Request $request)
    {
        $id = $request->post('id');
        if (empty($id)) {
            return $this->writeJson(500, null, '参数错误');
        }

        Goods::destroy($id);
        return $this->writeJson(0, null, '删除成功');
    }

    // 修改审核状态
    public function setStatus(Request $request)
    {
        $id = $request->post('id');
        $status = $request->post('status');
        if (empty($id)) {
            return $this->writeJson(500, null, '参数错误');
        }
        if (!in_array($status, ['1', '2'])) {
            return $this->writeJson(500, null, '审核状态码错误');
        }

        $model = Goods::find($id);
        if ($model->status != 0) {
            return $this->writeJson(500, null, '已审核，不能再审核');
        }
        $model->status = $status;
        $model->save();
        return $this->writeJson(0, null, '审核成功');
    }

    // 修改推荐状态
    public function setRec(Request $request)
    {
        $id = $request->post('id');
        $is_rec = $request->post('is_rec');
        if (empty($id)) {
            return $this->writeJson(500, null, '参数错误');
        }
        if (!in_array($is_rec, ['0', '1'])) {
            return $this->writeJson(500, null, '推荐设置错误');
        }
        $model = Goods::find($id);
        $model->is_rec = $is_rec;
        $model->save();
        return $this->writeJson(0, null, '设置成功');
    }
}
